package com.zyl.blog.controller;/**
 * @author 14323
 * @date 2020/8/12 15:02
 */

import com.zyl.blog.pojo.Type;
import com.zyl.blog.service.BlogService;
import com.zyl.blog.service.TypeService;
import com.zyl.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2020年08月12日 15:02
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@PathVariable Long id,
                        @PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        Model model) {

        List<Type> typeList = typeService.getTopType(1000);
        if (id == -1) {
            //如果id为-1说明是从首页跳转过来的，那么就默认选择第一个type
            id = typeList.get(0).getId();
        }

        model.addAttribute("types", typeList);
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("page", blogService.getBlogPage(pageable,blogQuery));
        model.addAttribute("activeTypeId", id);

        return "types";
    }
}
